package leetcode;

import java.util.Arrays;

public class LeetCode4 {

    public static void main(String[] args) {
        LeetCode4 leetCode4 = new LeetCode4();
        System.out.println(leetCode4.findMedianSortedArrays(new int[]{1, 2}, new int[]{3, 4}));
    }

    //要求算法的时间复杂度为 O(log(m + n))
    //不能顺序遍历

    //不考虑时间复杂度
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n = nums1.length, m = nums2.length, i = 0, j = 0, index = 0;
        double[] result = new double[n + m];
        while (i < n && j < m) {
            result[index++] = nums1[i] < nums2[j] ? nums1[i++] : nums2[j++];
        }
        while (i < n)
            result[index++] = nums1[i++];
        while (j < m)
            result[index++] = nums2[j++];
        return (n + m) % 2 != 0 ? result[(n + m) / 2] : (result[(n + m) / 2 - 1] + result[(n + m) / 2]) / 2;
    }
//------------------------------------------------------------------
//先不玩困难的

}
